﻿using System;
using System.Text;
using System.Threading.Tasks;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http;

namespace Kugar.UI.Web.AdminLTE.Authentications
{
    public class AdminJWTOption
    {
        public CookieBuilder Cookie { set; get; } = new CookieBuilder(){SameSite = SameSiteMode.Lax};

        public string AuthenticationScheme { set; get; } = "admin";

        /// <summary>
        /// token校验成功后,触发该回调,如果回调中,需要登录失败,调用context的Fail函数,会触发OnChallenge回调
        /// </summary>
        public Func<TokenValidatedContext, Task> OnTokenValidated { set; get; }

        /// <summary>
        /// 登录失败时,触发该回调,如需要触发跳转,使用context.Response.Redirect,后使用context.HandleResponse()中止后续处理
        /// </summary>
        public Func<JwtBearerChallengeContext, Task> OnChallenge { set; get; }

        private string _tokenEncKey;

        public string Issuer { set; get; } = "adminlte";

        public string Audience { set; get; } = "api";

        /// <summary>
        /// 用于存储加密cookie中jwt的密钥,必填
        /// </summary>
        public string TokenEncKey
        {
            set
            {
                _tokenEncKey = value;
                ActualEncKey = Encoding.UTF8.GetBytes(value.PadRight(128, '0'));
            }
            get => _tokenEncKey;
        }

        /// <summary>
        /// 登录页面的注册按钮标题
        /// </summary>
        public string RegisterTitle { set; get; }

        /// <summary>
        /// 登录页面的注册用户页面地址
        /// </summary>
        public string RegisterUrl { set; get; }

        /// <summary>
        /// 登录页面的忘记密码按钮标题
        /// </summary>
        public string ForgetpasswordTitle { set; get; }

        /// <summary>
        /// 登录页面的忘记密码页Url
        /// </summary>
        public string ForgetpasswordUrl { set; get; }

        /// <summary>
        /// 登录页面的扩展管理类接口
        /// </summary>
        public ILoginPageManager LoginPageManager { get; set; }

        /// <summary>
        /// 用户登录管理接口
        /// </summary>
        public IAdminLoginManager LoginManager { set; get; }

        /// <summary>
        /// 所有页面title参数的前缀
        /// </summary>
        public string PrefixTitle { set; get; } = "";

        public string MainTitle { set; get; } = "管理后台";

        /// <summary>
        /// 登录页面的子标题
        /// </summary>
        public string SubTitle { set; get; } = "";

        /// <summary>
        /// 登录页面的LogoUrl
        /// </summary>
        public string LogoUrl { set; get; } = "";

        /// <summary>
        /// cookie的过期时间,默认位7天
        /// </summary>
        public TimeSpan ExpireTimeSpan { set; get; } = TimeSpan.FromDays(7);

        /// <summary>
        /// 默认自带的页面的Layout文件路径(相对路径),该参数用于设置如:忘记密码,权限管理,后台登录人员页面的Layout
        /// </summary>
        public string EmbeddedPageLayout{ set; get; }

        /// <summary>
        /// 实际使用的EncKey,不直接使用
        /// </summary>
        public byte[] ActualEncKey { get; private set; }

        /// <summary>
        /// 自定义 登录链接地址
        /// </summary>
        public string LoginUrl { set; get; }

    }
}